package com.example.archive.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.archive.entity.RestrictedArchiveRequest;
import com.example.archive.entity.User;
import com.example.archive.service.RestrictedArchiveRequestService;
import com.example.archive.service.UserService;
import com.example.archive.util.PageParam;
import com.example.archive.util.Result;
import com.example.archive.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 受限档案访问申请控制器
 * 提供受限档案申请的前端接口
 */
@RestController
@RequestMapping("/restricted-archive-request")
@CrossOrigin
public class RestrictedArchiveRequestController {
    
    @Autowired
    private RestrictedArchiveRequestService requestService;
    
    @Autowired
    private UserService userService;
    
    /**
     * 提交受限档案访问申请
     * @param request 申请对象
     * @return 提交结果
     */
    @PostMapping("/submit")
    public Result<Boolean> submitRequest(@RequestBody RestrictedArchiveRequest request) {
        Integer currentUserId = UserContext.getUserId();
        if (currentUserId == null) {
            return Result.fail(401, "用户未登录");
        }
        
        // 设置申请人
        request.setUserId(currentUserId);
        
        return requestService.submitRequest(request);
    }
    
    /**
     * 分页查询受限档案申请列表
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @param status 审批状态
     * @param userId 用户ID
     * @param approverId 审批人ID
     * @param search 搜索关键词
     * @return 申请列表
     */
    @GetMapping("/list")
    public Result<IPage<RestrictedArchiveRequest>> listRequests(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) Integer approverId,
            @RequestParam(required = false) String search) {
        
        Integer currentUserId = UserContext.getUserId();
        if (currentUserId == null) {
            return Result.fail(401, "用户未登录");
        }
        
        // 获取当前用户角色，进行权限控制
        Result<User> userResult = userService.getUserById(currentUserId);
        if (userResult.getCode() != 200 || userResult.getData() == null) {
            return Result.fail(401, "无法获取用户信息");
        }
        String userRole = userResult.getData().getRole();
        
        Integer effectiveUserId = null;
        Integer effectiveApproverId = null;
        
        // 根据角色进行过滤
        if ("普通员工".equals(userRole)) {
            // 普通员工只能看自己提交的申请
            effectiveUserId = currentUserId;
        } else if ("企业领导".equals(userRole)) {
            // 企业领导可以看所有申请，或者指定查看某个用户的申请
            effectiveUserId = userId;
            effectiveApproverId = approverId;
        } else if ("档案管理员".equals(userRole)) {
            // 档案管理员可以查看所有申请（用于了解申请情况）
            effectiveUserId = userId;
            effectiveApproverId = approverId;
        }
        
        PageParam pageParam = new PageParam(pageNum, pageSize, search);
        return requestService.listRequests(pageParam, status, effectiveUserId, effectiveApproverId);
    }
    
    /**
     * 审批受限档案访问申请
     * @param requestId 申请ID
     * @param approvalStatus 审批状态
     * @param notes 审批备注
     * @return 审批结果
     */
    @PutMapping("/approve/{requestId}")
    public Result<Boolean> approveRequest(
            @PathVariable Integer requestId,
            @RequestParam String approvalStatus,
            @RequestParam(required = false) String notes) {
        
        Integer currentUserId = UserContext.getUserId();
        if (currentUserId == null) {
            return Result.fail(401, "用户未登录");
        }
        
        // 检查用户角色
        Result<User> userResult = userService.getUserById(currentUserId);
        if (userResult.getCode() != 200 || userResult.getData() == null) {
            return Result.fail(401, "无法获取用户信息");
        }
        String userRole = userResult.getData().getRole();
        
        if (!"企业领导".equals(userRole)) {
            return Result.fail(403, "只有企业领导可以审批受限档案访问申请");
        }
        
        return requestService.approveRequest(requestId, currentUserId, approvalStatus, notes);
    }
    
    /**
     * 获取申请详情
     * @param requestId 申请ID
     * @return 申请详情
     */
    @GetMapping("/detail/{requestId}")
    public Result<RestrictedArchiveRequest> getRequestDetail(@PathVariable Integer requestId) {
        return requestService.getRequestDetail(requestId);
    }
} 